package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.util.DepartmentQuery;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * @param department
     * @return
     * @RequestMapping(value = "/save",method = RequestMethod.PUT)
     * 快捷键生成try catch  CREL+ALT +T
     */
    @PutMapping
    public AjaxResult save(@RequestBody Department department) {
        try {
            System.out.println("添加："+department);
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * @param department
     * @return
     * @RequestMapping(value = "/update",method = RequestMethod.POST)
     */
    @PostMapping
    public AjaxResult update(@RequestBody Department department) {
        try {
            System.out.println(department);
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     *@RequestMapping(value = "/delete",method = RequestMethod.Delete)
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            System.out.println("前台传入的ID是:"+id);
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, e.getMessage());
        }
    }

    @PatchMapping(value="/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Department> departments) {
        try {
            //获取到对象集合，从中得到id,批量删除
            departmentService.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
    }
    /**
     * @RequestMapping(value = "/loadOne",method = RequestMethod.GET)
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Department loadOne(@RequestBody @PathVariable("id") Long id){
        Department department = departmentService.loadOne(id);
        return department;
    }

    @PatchMapping(value="/loadParentAll")
    public List<Department> loadParentAll(){
        List<Department> list = departmentService.loadParentAll();
        return list ;
    }
    /**
     * @RequestMapping(value = "/loadAll",method = RequestMethod.Patch)
     * @return
     */
    /*接受前台传入的参数 要加上@RequestBody*/
    @PatchMapping
    public PageInfo<Department> findAll(@RequestBody DepartmentQuery departmentQuery){
        System.out.println(departmentQuery);
        return departmentService.findAll(departmentQuery);
    }
}
